package com.imay.live.view.custom;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.imay.live.R;

import cn.dreamtobe.kpswitch.util.KPSwitchConflictUtil;
import cn.dreamtobe.kpswitch.util.KeyboardUtil;
import cn.dreamtobe.kpswitch.widget.KPSwitchPanelLinearLayout;

/**
 * Created by jeaman on 2017/6/22.
 * 超短播自定义题目开播view
 */

public class StartLiveShortCustomView extends StartLiveView implements KeyboardUtil.OnKeyboardShowingListener {
    private EditText mTitleEt;
    private EditText mContentEt;
    private TextView mTitleLimitTv;
    private TextView mContentLimitTv;
    private TextView mTipsTv;
    private KPSwitchPanelLinearLayout mTitlePanelRoot;
    private KPSwitchPanelLinearLayout mContentPanelRoot;

    public StartLiveShortCustomView(Context context) {
        super(context);
    }

    public StartLiveShortCustomView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public StartLiveShortCustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public StartLiveShortCustomView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.view_start_live_short_custom;
    }

    @Override
    protected void init(Context context) {
        super.init(context);
        mTitleEt = (EditText) findViewById(R.id.start_live_short_et_title);
        mContentEt = (EditText) findViewById(R.id.start_live_short_et_content);
        mTitleLimitTv = (TextView) findViewById(R.id.start_live_short_tv_title_limit);
        mContentLimitTv = (TextView) findViewById(R.id.start_live_short_tv_content_limit);
        mTipsTv = (TextView) findViewById(R.id.start_live_short_tv_tips);
        mTitlePanelRoot = (KPSwitchPanelLinearLayout) findViewById(R.id.start_live_short_title_sp_panel);
        mContentPanelRoot = (KPSwitchPanelLinearLayout) findViewById(R.id.start_live_short_content_sp_panel);

        initListener();
        setBGMViewVisible(true);
    }

    private void initListener() {
        mTitleEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                mTitleLimitTv.setText(String.valueOf(10 - s.length()));
                setStartButtonStatus();
            }
        });

        mContentEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                mContentLimitTv.setText(String.valueOf(50 - s.length()));
                setStartButtonStatus();
            }
        });
    }

    private void setStartButtonStatus() {
        if (getTitle().length() > 0 && getContent().length() > 0) {
            mStartBtn.setEnabled(true);
            mTipsTv.setVisibility(VISIBLE);
        } else {
            mStartBtn.setEnabled(false);
        }
    }

    public void hideKeyboard() {
        KeyboardUtil.hideKeyboard(mTitleEt);
        KeyboardUtil.hideKeyboard(mContentEt);
        mTitlePanelRoot.setVisibility(View.GONE);
        mContentPanelRoot.setVisibility(View.GONE);
    }

    public void attachKeyboard(Activity activity) {
        KeyboardUtil.attach(activity, mTitlePanelRoot, this);
        KPSwitchConflictUtil.attach(mTitlePanelRoot, null, mTitleEt);

        KeyboardUtil.attach(activity, mContentPanelRoot, this);
        KPSwitchConflictUtil.attach(mContentPanelRoot, null, mContentEt);
    }

    public String getTitle() {
        return mTitleEt.getText().toString().trim();
    }

    public String getContent() {
        return mContentEt.getText().toString().trim();
    }

    @Override
    public void onKeyboardShowing(boolean isShowing) {
        if (!isShowing) {
            hideKeyboard();
            setOnClickListener(null);
        } else {
            setOnClickListener(this);        //点击隐藏输入法
        }
    }

    @Override
    public void onClick(View v) {
        if (v == this) {
            hideKeyboard();
        } else {
            super.onClick(v);
        }
    }
}
